import Vue from 'vue'
import Vuex from 'vuex'
import Axios from 'axios'
import api from '../api/api'
Vue.use(Vuex);

const store =new Vuex.Store({
    state: {
        audio:{
            'id': 0,
            'name': '歌曲名称',
            'singer': '演唱者',
            'albumPic': '',
            'location':require('../assets/images/player-bar.png'),
            'album': ''
        },
        lyric: '正在加载中。。',
        currentIndex: 0, // 当前播放的歌曲位置
        playing: false, // 是否正在播放
        loading: false, // 是否正在加载中
        showDetail: false,
        songList: [],    // 播放列表
        currentTime: 0,
        tmpCurrentTime: 0,
        durationTime: 0,//返回的歌曲的长度
        bufferedTime: 0,//缓存时间
        change: false   // 判断是更改的时间还是播放的时间
    },
    getters:{
        audio: state => state.audio,    //歌曲内容
        playing: state => state.playing, //是否播放
        loading: state => state.loading,    //是否加载中
        showDetail: state => state.showDetail, //是否显示
        durationTime: state => state.durationTime, //
        currentIndex: state => state.currentIndex, //当前播放
        bufferedTime: state => state.bufferedTime,//缓冲时间
        tmpCurrentTime: state => state.tmpCurrentTime,
        songList: state => state.songList, //播放列表
        currentTime: state => state.currentTime,//当前播放进度
        change: state => state.change,
        prCurrentTime: state => {//计算当前播放进度
        return state.currentTime / state.durationTime * 100
        },
        prBufferedTime: state => {//计算缓存进度
        return state.bufferedTime / state.durationTime * 100
        }
    },
    mutations:{
        openLoading (state) {
            state.loading = true
        },
        addToList(state,item){
            //判读歌曲是否有重复
            var flag =false
            state.songList.forEach(function(ele,index) {
                if(ele.id===item.id){
                    flag=true
                    state.currentIndex = index+1;
                }
            });
            //不重复就添加进歌曲数组里面
            if(!flag){
                state.songList.push(item)
                state.currentIndex=state.songList.length
            }
        },
        pause(state){
            state.playing=false
        },
        setAudio(state){
            state.audio = state.songList[state.currentIndex-1]
        },
        setLocation(state,location){
            state.audio.location=location
        },
        closeLoading (state) {
            state.loading = false
        },
        play (state) {
            state.playing = true
        },
        playNext(state){
            state.currentIndex++
            if(state.currentIndex>state.songList.length){
                state.currentIndex=1
            }
            state.audio = state.songList[state.currentIndex-1]
        },
        playPre(state){
            state.currentIndex--
            if(state.currentIndex<1){
                state.currentIndex=state.songList.length
            }
            state.audio=state.songList[state.currentIndex-1]
        },
        updateBufferedTime(state,time){
            state.bufferedTime = time
        },
        updateDurationTime(state,time){
            state.durationTime = time
        },
        updateCurrentTime(state,time){
            state.currentTime =time
        },
        removeAudio(state,index){
            state.songList.splice(index,1)
            if (index === state.songList.length) {
                index--
            }
            state.audio = state.songList[index]
            state.currentIndex = index + 1
            if (state.songList.length === 0) {
                state.audio = {
                'id': 0,
                'name': '歌曲名称',
                'singer': '演唱者',
                'albumPic': '/static/player-bar.png',
                'location': '',
                'album': ''
                }
                state.playing = false
            }
        },
        setAudioIndex(state,index){
            state.audio = state.songList[index]
            state.currentIndex = index + 1
        },
        toggleDetail(state){
            state.showDetail=!state.showDetail;
        },
        changeTime(state,time){
            state.tmpCurrentTime=time
        },
        setChange(state,flag){
            state.change = flag
        }
    },
    //异步操作数据的操作
    actions: {
        getSong({commit,state},id){
            commit("openLoading")
            Axios.get(api.getSong(id)).then(res=>{
                var url=res.data.data[0].url
                commit('setAudio')
                commit('setLocation', url)
            })
        }
    }
})
export default store